package ru.rs.framework.objects;

import ru.rs.framework.interfaces.Game;
import ru.rs.framework.interfaces.Graphics;
import ru.rs.framework.interfaces.impl.GraphicsImpl;
import android.app.Activity;
import android.content.res.Configuration;
import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.os.Bundle;
import android.view.Window;
import android.view.WindowManager;

public abstract class AbstractActivity extends Activity implements Game {

	private RenderView view;
	private Screen screen;
	private Graphics graphic;

	@Override
	protected void onCreate(Bundle savedInstanceState) {

		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
				WindowManager.LayoutParams.FLAG_FULLSCREEN);
		boolean isLand = getResources().getConfiguration().orientation//
		== Configuration.ORIENTATION_LANDSCAPE;
		int w = isLand ? 480 : 320, h = isLand ? 320 : 480;

		Bitmap frame = Bitmap.createBitmap(w, h, Config.RGB_565);

		view = new RenderView(this, frame);
		graphic = new GraphicsImpl(getAssets(), frame);
		screen = getStartScreen();
		setContentView(view);

	}

	@Override
	public Screen getCurrentScreen() {
		return screen;
	}

	@Override
	public void setScreen(Screen scrn) {
		if (scrn != null) {
			screen.pause();
			screen.dispose();
			screen = scrn;
			screen.resume();
			screen.update(0);

		} else
			throw new IllegalArgumentException(
					"No fucking way with null screen!");
	}

	@Override
	public abstract Screen getStartScreen();

	@Override
	protected void onPause() {
		super.onPause();
		view.pause();
		screen.pause();
		if (isFinishing()) {
			screen.dispose();
		}
	}

	@Override
	protected void onResume() {
		super.onResume();
		screen.resume();
		view.resume();
	}

	/*
	 * @see ru.rs.framework.interfaces.Game#getGraphic()
	 */
	@Override
	public Graphics getGraphic() {
		return graphic;
	}

}
